package com.baizhi.serviceImpl;


import com.baizhi.dao.UserDao;
import com.baizhi.dto.PageDTO;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.util.AliyunOSSUtil;
import com.baizhi.vo.CommonMessageVO;
import com.baizhi.vo.CommonVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
    private String fileName = null;

    @Autowired
    private UserDao userDao;

    @Override
    public CommonVO pageQueryAll(PageDTO pageDTO) {
        Page<User> pages = new Page<>(pageDTO.getPage(), pageDTO.getPageSize());
        Page<User> selectPage = userDao.selectPage(pages, null);
        List<User> records = selectPage.getRecords();
        long total = selectPage.getTotal();
        CommonVO vo = new CommonVO(pageDTO.getPage(),total,records);
        return vo;
    }

    /**
     * 根据id查
     *
     * @param user
     */
    @Override
    public User queryById(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,user.getId());
        User users = userDao.selectOne(queryWrapper);
        return users;
    }

    @Override
    public CommonMessageVO update(User user) {
        try {
            int i = userDao.updateById(user);
            return CommonMessageVO.succeed();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonMessageVO.failed();
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public CommonMessageVO delete(User user) {
        try {
            /**
             * 删除前根据传递过来的id查一个，再获取地址进行删除
             */
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getId,user.getId());
            User users = userDao.selectOne(queryWrapper);
            System.out.println("users = " + users);
            String headImg = users.getHeadImg();
            System.out.println("headImg = " + headImg);
            /**
             * 字符串拆分
             */
            String path ="https://yingx20210827.oss-cn-beijing.aliyuncs.com/";
            String[] split = headImg.split(path);
            for (String s : split) {
                System.out.println("s = " + s);
            }
            int i = userDao.deleteById(user);
            AliyunOSSUtil.deleteBucket("yingx20210827",split[1]);
            return CommonMessageVO.succeed();
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return CommonMessageVO.failed();
        }
    }

    /**
     * 添加新用户
     *
     * @param user
     */
    @Override
    public CommonMessageVO insert(User user) {
        try {
            //UserServiceImpl userService = new UserServiceImpl();
            //https://yingx20210827.oss-cn-beijing.aliyuncs.com/image/1630123138335_%E7%B1%BB%E5%9B%BE.gif
            //https://yingx20210827.oss-cn-beijing.aliyuncs.com/
            String path =" https://yingx20210827.oss-cn-beijing.aliyuncs.com/";
            String imgPath = path+fileName;
            user.setHeadImg(imgPath);
            int insert = userDao.insert(user);
            return CommonMessageVO.succeed();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonMessageVO.failed();
        }
    }

    /**
     * 文件上传至阿里云
     *
     * @param headImg 要上传的文件
     */
    @Override
    public String uploadHeadImg(MultipartFile headImg) {
        String contentType = headImg.getContentType();
        String[] split = contentType.split("/");
        if (split[0].equals("image")){
            //该文件是图片
            fileName = split[0]+"/"+System.currentTimeMillis()+"_"+headImg.getOriginalFilename();;
        }if (split[0].equals("video")){
            //该文件是视频
            fileName = split[0]+"/"+System.currentTimeMillis()+"_"+headImg.getOriginalFilename();;
        }
        AliyunOSSUtil.uploadfileBytes("yingx20210827",fileName,headImg);
        return fileName;
    }
}